SpringBoot实现MultipartFile文件上传

您所在的位置:网站首页 上传文件 springboot SpringBoot实现MultipartFile文件上传

SpringBoot实现MultipartFile文件上传

2023-04-22 23:59| 来源: 网络整理| 查看: 265

1、SpringBoot采用FileUpload组件实现上传处理,在控制器中可以使用MultipartFile类进行接收。

1 2 6 4.0.0 7 8 org.springframework.boot 9 spring-boot-starter-parent 10 2.3.5.RELEASE 11 12 13 com.example 14 demo 15 0.0.1-SNAPSHOT 16 demo 17 Demo project for Spring Boot 18 19 20 1.8 21 3.1.1 22 23 24 25 26 org.springframework.boot 27 spring-boot-starter-web 28 29 30 31 org.springframework.boot 32 spring-boot-starter-test 33 test 34 35 36 org.junit.vintage 37 junit-vintage-engine 38 39 40 41 42 43 org.hibernate 44 hibernate-validator 45 6.1.0.Final 46 47 48 org.springframework.boot 49 spring-boot-starter-thymeleaf 50 51 52 53 54 55 56 org.springframework.boot 57 spring-boot-maven-plugin 58 59 60 61 62 src/main/resources 63 64 **/*.properties 65 **/*.yml 66 **/*.xml 67 **/*.p12 68 **/*.html 69 **/*.jpg 70 **/*.png 71 72 73 74 75 76

创建控制器类UploadController,利用MultipartFile将上传文件保存在本地磁盘。

1 package com.demo.controller; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.HashMap; 6 import java.util.Map; 7 import java.util.UUID; 8 9 import org.springframework.stereotype.Controller; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMethod; 12 import org.springframework.web.bind.annotation.RequestParam; 13 import org.springframework.web.bind.annotation.ResponseBody; 14 import org.springframework.web.context.request.RequestAttributes; 15 import org.springframework.web.context.request.RequestContextHolder; 16 import org.springframework.web.context.request.ServletRequestAttributes; 17 import org.springframework.web.multipart.MultipartFile; 18 19 /** 20 * 21 * @author 22 * 23 */ 24 @Controller 25 public class UploadController { 26 27 /** 28 * 首先根据此方法跳转到upload.html界面 29 * 30 * @return 31 */ 32 @RequestMapping(value = "/upload_pre", method = RequestMethod.GET) 33 public String uploadPre() { 34 return "upload"; 35 } 36 37 /** 38 * 文件上传 39 * 40 * @param name 41 * @param multipartFile 42 * @return 43 * @throws IllegalStateException 44 * @throws IOException 45 */ 46 @RequestMapping(value = "/upload", method = RequestMethod.POST) 47 @ResponseBody 48 public Object upload(String name, @RequestParam(value = "multipartFile") MultipartFile multipartFile) 49 throws IllegalStateException, IOException { 50 Map map = new HashMap(); 51 if (multipartFile != null) { 52 // 设置文件名称 53 map.put("nameParam", name); 54 // 设置文件名称 55 map.put("fileame", multipartFile.getName()); 56 // 设置文件类型 57 map.put("contentType", multipartFile.getContentType()); 58 // 设置文件大小 59 map.put("fileSize", multipartFile.getSize()); 60 // 创建文件名称 61 String fileName = UUID.randomUUID() + "." 62 + multipartFile.getContentType().substring(multipartFile.getContentType().lastIndexOf("/") + 1); 63 // 获取到文件的路径信息 64 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); 65 ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes; 66 String filePath = servletRequestAttributes.getRequest().getServletContext().getRealPath("/") + fileName; 67 // 打印保存路径 68 System.out.println(filePath); 69 // 保存文件的路径信息 70 map.put("filePath", filePath); 71 // 创建文件 72 File saveFile = new File(filePath); 73 // 文件保存 74 multipartFile.transferTo(saveFile); 75 // 返回信息 76 return map; 77 } else { 78 return "no file "; 79 } 80 } 81 82 }

在src/main/resources/templates下面创建upload.html文件。

1 2 3 4 5 搞事情啊!!! 6 7 8 搞事情啊!!! 9 10 11 姓名: 12 13 照片: 14 15 16 17 18

控制层类UploadController接收到此请求信息后,如果有上传文件存在,则会直接返回上传信息(开发者也可以根据情况选择将文件保存)。

演示效果,如下所示:

2、Springboot上传文件限制,在实际项目开发中,需要对用户上传文件的大小进行限制,这样才可以保证服务器的资源不被浪费。修改application.properties配置文件,如下所示:

1 # 启用http上传 2 spring.servlet.multipart.enabled=true 3 # 设置支持的单个上传文件的大小限制 4 spring.servlet.multipart.max-file-size=10MB 5 # 设置最大的请求文件大小,设置总体大小请求 6 spring.servlet.multipart.max-request-size=20MB 7 # 当上传文件达到指定配置量的时候,将文件内容写入磁盘 8 spring.servlet.multipart.file-size-threshold=512KB 9 # 设置上传的临时目录 10 spring.servlet.multipart.location=/

对于Springboot上传文件的上传限制,也可以利用Bean实现同样的效果,实现代码,如下所示:

1 package com.demo.config; 2 3 import javax.servlet.MultipartConfigElement; 4 5 import org.springframework.boot.web.servlet.MultipartConfigFactory; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.context.annotation.Configuration; 8 import org.springframework.util.unit.DataSize; 9 10 @Configuration 11 public class UploadConfig { 12 13 @Bean 14 public MultipartConfigElement getMultipartConfig() { 15 MultipartConfigFactory config = new MultipartConfigFactory(); 16 // 设置上传文件的单个大小限制 17 config.setMaxRequestSize(DataSize.parse("10MB")); 18 // 设置总的上传的大小限制 19 config.setMaxRequestSize(DataSize.parse("100MB")); 20 // 设置临时保存目录 21 config.setLocation("/"); 22 // 创建一个上传配置并返回 23 return config.createMultipartConfig(); 24 } 25 26 }

3、Springboot上传多个文件如果要进行多个文件的上传,需要通过MultipartHttpServletRequest进行文件接收。首先修改upload.html页面,定义多个文件上传控件。

1 2 3 4 5 搞事情啊!!! 6 7 8 搞事情啊!!! 9 10 11 姓名: 12 13 文件一: 14 文件二: 15 文件三: 16 17 18 19 20

修改UploadController.java控制器,实现多个文件上传,此时可以根据接受参数的不同,方法可以有不同的实现 ,如果接受参数是这样的,@RequestParam(value = "multipartFile") MultipartFile[] multipartFile,实现代码,如下所示:

1 package com.demo.controller; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.HashMap; 6 import java.util.Map; 7 import java.util.UUID; 8 9 import org.springframework.stereotype.Controller; 10 import org.springframework.web.bind.annotation.RequestMapping; 11 import org.springframework.web.bind.annotation.RequestMethod; 12 import org.springframework.web.bind.annotation.RequestParam; 13 import org.springframework.web.bind.annotation.ResponseBody; 14 import org.springframework.web.context.request.RequestAttributes; 15 import org.springframework.web.context.request.RequestContextHolder; 16 import org.springframework.web.context.request.ServletRequestAttributes; 17 import org.springframework.web.multipart.MultipartFile; 18 19 /** 20 * 21 * @author 22 * 23 */ 24 @Controller 25 public class UploadController { 26 27 /** 28 * 首先根据此方法跳转到upload.html界面 29 * 30 * @return 31 */ 32 @RequestMapping(value = "/upload_pre", method = RequestMethod.GET) 33 public String uploadPre() { 34 return "upload"; 35 } 36 37 /** 38 * 文件上传 39 * 40 * @param name 41 * @param multipartFile 42 * @return 43 * @throws IllegalStateException 44 * @throws IOException 45 */ 46 @RequestMapping(value = "/upload", method = RequestMethod.POST) 47 @ResponseBody 48 public Object upload(String name, @RequestParam(value = "multipartFile") MultipartFile[] multipartFile) 49 throws IllegalStateException, IOException { 50 Map map = new HashMap(); 51 if (multipartFile != null && multipartFile.length > 0) { 52 for (int i = 0; i < multipartFile.length; i++) { 53 // 设置文件名称 54 map.put("nameParam", name); 55 // 设置文件名称 56 map.put("fileame", multipartFile[i].getName()); 57 // 设置文件类型 58 map.put("contentType", multipartFile[i].getContentType()); 59 // 设置文件大小 60 map.put("fileSize", multipartFile[i].getSize()); 61 // 创建文件名称 62 String fileName = UUID.randomUUID() + "." + multipartFile[i].getContentType() 63 .substring(multipartFile[i].getContentType().lastIndexOf("/") + 1); 64 // 获取到文件的路径信息 65 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); 66 ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes; 67 String filePath = servletRequestAttributes.getRequest().getServletContext().getRealPath("/") + fileName; 68 // 打印保存路径 69 System.out.println(filePath); 70 // 保存文件的路径信息 71 map.put("filePath", filePath); 72 // 创建文件 73 File saveFile = new File(filePath); 74 // 文件保存 75 multipartFile[i].transferTo(saveFile); 76 // 返回信息 77 } 78 return map; 79 } else { 80 return "no file "; 81 } 82 } 83 84 }

如果接受参数是HttpServletRequest request,代码是这样的,明显没有第一种方便,如下所示:

1 package com.demo.controller; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.Iterator; 8 import java.util.List; 9 import java.util.Map; 10 import java.util.UUID; 11 12 import javax.servlet.http.HttpServletRequest; 13 14 import org.springframework.stereotype.Controller; 15 import org.springframework.web.bind.annotation.RequestMapping; 16 import org.springframework.web.bind.annotation.RequestMethod; 17 import org.springframework.web.bind.annotation.RequestParam; 18 import org.springframework.web.bind.annotation.ResponseBody; 19 import org.springframework.web.context.request.RequestAttributes; 20 import org.springframework.web.context.request.RequestContextHolder; 21 import org.springframework.web.context.request.ServletRequestAttributes; 22 import org.springframework.web.multipart.MultipartFile; 23 import org.springframework.web.multipart.MultipartHttpServletRequest; 24 25 /** 26 * 27 * @author 28 * 29 */ 30 @Controller 31 public class UploadController { 32 33 /** 34 * 首先根据此方法跳转到upload.html界面 35 * 36 * @return 37 */ 38 @RequestMapping(value = "/upload_pre", method = RequestMethod.GET) 39 public String uploadPre() { 40 return "upload"; 41 } 42 43 /** 44 * 文件上传 45 * 46 * @param name 47 * @param multipartFile 48 * @return 49 * @throws IllegalStateException 50 * @throws IOException 51 */ 52 @RequestMapping(value = "/upload", method = RequestMethod.POST) 53 @ResponseBody 54 public Object upload2(String name, HttpServletRequest request) throws IllegalStateException, IOException { 55 List result = new ArrayList(); 56 if (request instanceof MultipartHttpServletRequest) { 57 MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request; 58 List files = multipartHttpServletRequest.getFiles("multipartFile"); 59 Iterator iterator = files.iterator(); 60 while (iterator.hasNext()) { 61 // 取出每一个文件 62 MultipartFile file = iterator.next(); 63 try { 64 // 保存上传信息 65 result.add(this.saveFile(file)); 66 } catch (Exception e) { 67 e.printStackTrace(); 68 } 69 } 70 } 71 return result; 72 } 73 74 /** 75 * 文件保存处理 76 * 77 * @param file 78 * 上传文件 79 * @return 文件保存路径 80 * @throws Exception 81 * 上传异常 82 */ 83 public String saveFile(MultipartFile multipartFile) throws Exception { 84 String filePath = "no file!!!"; 85 if (multipartFile != null && multipartFile.getSize() > 0) { 86 // 创建文件名称 87 String fileName = UUID.randomUUID() + "." 88 + multipartFile.getContentType().substring(multipartFile.getContentType().lastIndexOf("/") + 1); 89 // 获取到文件的路径信息 90 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); 91 ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes; 92 filePath = servletRequestAttributes.getRequest().getServletContext().getRealPath("/") + fileName; 93 // 打印保存路径 94 System.out.println(filePath); 95 File saveFile = new File(filePath); 96 // 文件保存 97 multipartFile.transferTo(saveFile); 98 } 99 return filePath; 100 } 101 102 }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3